package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetilInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date ;
import java.util.List;

@Api(tags="01.资讯管理模块")
//@Controller
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
//@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("add")
    @ApiOperation("添加资讯")
    @ApiOperationSupport(order = 10)
    //添加咨询功能   1.根据文档确定是否需要VO类或者DTO类
//    2.梳理控制器方法的业务逻辑，确定接口的参数和返回值
    //3.完成接口方法和XML配置SQL
    //4.完成Controller中业务和接口的调用
    //5.重启工程测试。
    public JsonResult add(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam="+noticeAddParam);
        Notice notice = new Notice();
//        notice.setTitle(noticeAddParam.getTitle());
//        notice.setContent(noticeAddParam.getContent());
//        notice.setType(noticeAddParam.getType());
        //复制属性
        BeanUtils.copyProperties(noticeAddParam,notice);
        //处理其他属性
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }
    @GetMapping("list")
    @ApiOperation("资讯列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(NoticeAddQuery noticeAddQuery){
        System.out.println("noyiceAddQuery="+ noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOS = noticeMapper.list(title,type);
//        return new JsonResult(StatusCode.OPERATION_SUCCESS,noticeListVOS);
        return JsonResult.ok(noticeListVOS);
    }
    @GetMapping("detail")
    @ApiOperation("资讯详情")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name="id",value="资讯编号",required = true,dataType = "Long"),
            @ApiImplicitParam(name="title",value="资讯标题",required = true,dataType = "String")
    })

    public JsonResult detail(Long id,String title){
        System.out.println("detail="+id);
        NoticeDetilInfoVO noticeDetilInfoVO = noticeMapper.details(id);
        System.out.println("noticeDetilInfoVO="+noticeDetilInfoVO);
//        return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.ok();
    }
    @PostMapping("del")
    @ApiOperation("删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult delete(Long id){
        System.out.println("delete="+id);
        noticeMapper.deleteNoticeById(id);
//        return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("更新资讯")
    @ApiOperationSupport(order = 50)
    public JsonResult update(@ApiIgnore NoticeUpdateParam noticeUpdateParam){
        System.out.println("noticeUpdateParam="+noticeUpdateParam);
        //更新数据使用实体类
        Notice notice = new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        //设置更新时间
        notice.setUpdateTime(new Date());
        //调用接口方法
        int i = noticeMapper.updateNotice(notice);
        if(i>0){
//            return new JsonResult(StatusCode.OPERATION_SUCCESS);
            return JsonResult.ok();
        }
        else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
